package com.henry.chatkit.messages.holder

import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.RelativeLayout
import android.widget.TextView
import com.henry.chatkit.R
import com.henry.chatkit.CustomMessageViewGroup
import com.henry.chatkit.messages.MessageInfo
import com.henry.chatkit.utils.TimeUtils


class MessageCustomHolder(itemView: View) : MessageContentHolder(itemView), CustomMessageViewGroup {

    private var mMessage: MessageInfo? = null
    private var mPosition = 0

    private lateinit var msgBodyText: TextView

    override fun getVariableLayout(): Int {
        return R.layout.message_content_text
    }

    override fun initVariableViews() {
        msgBodyText = rootView.findViewById(R.id.msg_body_tv);
    }

    override fun layoutViews(msg: MessageInfo?, position: Int) {
        mMessage = msg;
        mPosition = position;
        super.layoutViews(msg, position)
    }

    // 因为recycleview的复用性，可能该holder回收后继续被custom类型的item复用
    // 但是因为addMessageContentView破坏了msgContentFrame的view结构，所以会造成items的显示错乱。
    // 这里我们重新添加一下msgBodyText
    override fun layoutVariableViews(msg: MessageInfo?, position: Int) {

        if (msg == null) {
            return
        }

        msgContentFrame.removeAllViews()
        if (msgBodyText.parent != null) {
            (msgBodyText.parent as ViewGroup).removeView(msgBodyText)
        }
        msgContentFrame.addView(msgBodyText)
        msgBodyText.visibility = View.VISIBLE

        msgContentFrame.visibility = View.VISIBLE

        if (properties.chatContextFontSize != 0) {
            msgBodyText.textSize = properties.chatContextFontSize.toFloat()
        }
        if (msg.isSelf) {
            if (properties.rightChatContentFontColor != 0) {
                msgBodyText.setTextColor(properties.rightChatContentFontColor)
            }
        } else {
            if (properties.leftChatContentFontColor != 0) {
                msgBodyText.setTextColor(properties.leftChatContentFontColor)
            }
        }

    }

    private fun hideAll() {
        for (i in 0 until (rootView as RelativeLayout).childCount) {
            (rootView as RelativeLayout).getChildAt(i).visibility = View.GONE
        }
    }

    override fun getContentFrame(): FrameLayout {
        return msgContentFrame
    }

    override fun getRootView(): RelativeLayout {
        return rootView as RelativeLayout
    }

    override fun addMessageItemView(view: View?) {
        hideAll()
        if (view != null) {
            (rootView as RelativeLayout).removeView(view)
            (rootView as RelativeLayout).addView(view)
        }
    }

    override fun addMessageContentView(view: View?) {
        // item有可能被复用，因为不能确定是否存在其他自定义view，这里把所有的view都隐藏之后重新layout
        hideAll()
        super.layoutViews(mMessage, mPosition)

        if (view != null) {
            for (i in 0 until msgContentFrame.childCount) {
                msgContentFrame.getChildAt(i).visibility = View.GONE
            }

            msgContentFrame.removeView(view)
            msgContentFrame.addView(view)
        }
    }

    override fun updateTime(timeText: TextView?) {
        if (timeText == null) {
            return
        }
        val time: Long = mMessage?.msgTime ?: 0
        if (properties.chatTimeBubble != null) {
            timeText.background = properties.chatTimeBubble
        }
        if (properties.chatTimeFontColor != 0) {
            timeText.setTextColor(properties.chatTimeFontColor)
        }
        if (properties.chatTimeFontSize != 0) {
            timeText.textSize = properties.chatTimeFontSize.toFloat()
        }

        if (mPosition > 1) {
            val lastTime: Long = mAdapter!!.getItem(mPosition - 1)?.msgTime ?: 0
            if (lastTime != 0L) {
                if (time - lastTime >= 5 * 60) {
                    timeText.visibility = View.VISIBLE
                    timeText.text = TimeUtils.getTime(time)
                } else {
                    timeText.visibility = View.GONE
                }
            } else {
                timeText.visibility = View.GONE
            }
        } else {
            timeText.visibility = View.VISIBLE
            timeText.text = TimeUtils.getTime(time)
        }
    }

}